#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 6000 + 5;
struct Rode
{
    int s, e, w;
} a[maxn];
int fa[maxn];
int cnt[maxn];
int n, m;
int Find(int x)
{
    return fa[x] == x ? x : x = Find(fa[x]);
}
bool cmp(Rode a, Rode b)
{
    return a.w < b.w;
}
int Kruskal()
{
    int ans = 0;
    for (int i = 1; i <= n; i++)
        fa[i] = i;
    sort(a + 1, a + m + 1, cmp);

    for (int i = 1; i <= m; i++)
    {
        int x = Find(a[i].s);
        int y = Find(a[i].e);
        if (x != y)
        {
            ans += a[i].w;
            fa[x] = y;
        }
    }
    return ans;
}
int main()
{
    while (cin >> n, n)
    {
        m = n * (n - 1) / 2;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d%d", &a[i].s, &a[i].e, &a[i].w);
        }
        cout << Kruskal() << endl;
    }
    return 0;
}
// https: //blog.csdn.net/hurmishine/article/details/52095527